package com.aliyuncs.aui.controller;

import com.aliyuncs.aui.dto.res.RestaurantInfo;
import com.aliyuncs.aui.dto.res.TableInfo;
import com.aliyuncs.aui.dto.res.CommonResponse;
import com.aliyuncs.aui.dto.res.MenuDTO;
import com.aliyuncs.aui.dto.res.OrdersCreateResponse;
import com.aliyuncs.aui.dto.res.ReservationResponse;
import com.aliyuncs.aui.dto.res.TakeoutResponse;
import com.aliyuncs.aui.dto.res.SessionResponse;
import com.aliyuncs.aui.dto.res.OrdersInfoResponse;
import com.aliyuncs.aui.dto.res.OrdersListResponse;

import org.springframework.web.bind.annotation.*;
import com.aliyuncs.aui.service.*;

import java.util.List;

@RestController
@RequestMapping("/api/resto")
public class RestoController {

    private final RestoService restoService;
    private final TablesService tablesService;
    private final ResvService resvService;
    private final TakeoutService takeoutService;
    private final SessionService sessionService;
    private final OrdersService ordersService;

    public RestoController(RestoService restoService, TablesService tablesService, 
            ResvService resvService, TakeoutService takeoutService, SessionService sessionService,
            OrdersService ordersService) {
        this.restoService = restoService;
        this.tablesService = tablesService;
        this.resvService = resvService;
        this.takeoutService = takeoutService;
        this.sessionService = sessionService;
        this.ordersService = ordersService;
    }

    /**
     * 生成会话唯一标识
     * @param tenantId 餐厅ID
     * @return 包含会话ID的响应
     */
    @GetMapping("/sessions/uuid")
    public CommonResponse generateSessionUuid() {
        return sessionService.generateSessionUuid();
    }

    /**
     * 创建新的会话
     * @param sessionId 会话ID
     * @param type 会话类型
     * @param role 角色
     * @param logContent 日志内容
     * @param tenantId 餐厅ID
     * @param userId 用户ID（可选，默认为system）
     * @return 操作结果
     */
    @PostMapping("/sessions")
    public CommonResponse createSession(
            @RequestParam String sessionId,
            @RequestParam String type,
            @RequestParam String role,
            @RequestParam String logContent,
            @RequestParam String tenantId,
            @RequestParam(required = false) String userId) {
        return sessionService.createSession(sessionId, type, role, logContent, tenantId, userId);
    }

    /**
     * 获取会话信息
     * @param sessionId 会话ID
     * @return 会话详细信息
     */
    @GetMapping("/sessions/{sessionId}")
    public SessionResponse getSession(
            @PathVariable String sessionId,
            @RequestParam String type) {
        return sessionService.getSession(sessionId, type);
    }

    /**
     * 创建新的外带订单
     * @param tenantId 餐厅ID
     * @param name 顾客姓名
     * @param addr 配送地址
     * @param tel 联系电话
     * @param state 初始状态
     * @return 操作结果
     */
    @PostMapping("/takeouts")
    public TakeoutResponse createTakeoutation(
            @RequestParam String tenantId,
            @RequestParam String name,
            @RequestParam String addr,
            @RequestParam String tel,
            @RequestParam String state) {
        return takeoutService.createTakeoutation(tenantId, name, addr, tel, state);
    }

    /**
     * 更新外带订单状态
     * @param takeoutId 外带订单ID
     * @param state 新状态
     * @return 操作结果
     */
    @PostMapping("/takeouts/{takeoutId}/state")
    public TakeoutResponse updateTakeoutationState(
            @PathVariable String takeoutId,
            @RequestParam String state) {
        return takeoutService.updateTakeoutationState(takeoutId, state);
    }

    /**
     * 获取餐厅完整信息
     * @param tenantId 餐厅唯一标识
     * @return 餐厅完整信息
     */
    @GetMapping("/{tenantId}/info")
    public RestaurantInfo getRestoInfo(@PathVariable String tenantId) {
        return restoService.getRestoInfo(tenantId);
    }

    /**
     * 查询可用餐桌
     * @param tenantId 餐厅ID
     * @param capacity 最大座位数（可选）
     * @param tableType 餐桌类型（可选，默认匹配所有类型）
     * @param tablePosition 餐桌位置（可选，默认匹配所有位置）
     * @return 可用餐桌列表
     */
    @GetMapping("/{tenantId}/tables")
    public List<TableInfo> getAvailableTables(
            @PathVariable String tenantId,
            @RequestParam(required = false) Integer capacity,
            @RequestParam(required = false) String tableType,
            @RequestParam(required = false) String tablePosition) {
        return tablesService.getAvailableTables(tenantId, capacity, tableType, tablePosition);
    }

    /**
     * 更新餐桌状态
     * @param tenantId 餐厅ID
     * @param tableNumber 餐桌编号
     * @param state 新状态（仅允许"占用"或"空闲"）
     * @return 操作结果
     */
    @PostMapping("/{tenantId}/tables/{tableNumber}/state/{state}")
    public CommonResponse updateTableState(
            @PathVariable String tenantId,
            @PathVariable String tableNumber,
            @PathVariable String state) {
        return tablesService.updateTableState(tenantId, tableNumber, state);
    }

    /**
     * 获取餐厅菜单
     * @param tenantId 餐厅ID
     * @return 菜单列表
     */
    @GetMapping("/{tenantId}/menus")
    public List<MenuDTO> getMenus(@PathVariable String tenantId) {
        return restoService.getMenusByTenantId(tenantId);
    }

    /**
     * 更新预定状态
     * @param tenantId 餐厅ID
     * @param state 新状态（已预定、已爽约、进行中、已结束）
     * @return 操作结果
     */
    @PostMapping("/reservations/{resvId}/state")
    public ReservationResponse updateReservationState(
            @PathVariable String resvId,
            @RequestParam String state) {
        return resvService.updateReservationState(resvId, state);
    }

    /**
     * 创建新的预定
     * @param tenantId 餐厅ID
     * @param resvTime 开始时间 (HH:mm格式)
     * @param tableNumber 餐桌编号
     * @param endTime 结束时间 (HH:mm格式，可选)
     * @param duration 间隔时间 (小时数，可选)
     * @param pax 预定人数 (可选)
     * @return 操作结果
     */
    @PostMapping("/reservations")
    public ReservationResponse createReservation(
            @RequestParam String tenantId,
            @RequestParam String resvTime,
            @RequestParam String tableNumber,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer duration,
            @RequestParam(required = false) Integer pax) {
        return resvService.createReservation(tenantId, resvTime, tableNumber, endTime, duration, pax);
    }

    /**
     * 创建新订单
     * @param tenantId 餐厅ID
     * @param status 订单状态
     * @return 操作结果
     */
    @PostMapping("/orders")
    public OrdersCreateResponse createOrders(
            @RequestParam String tenantId,
            @RequestParam String status) {
        return ordersService.createOrders(tenantId, status);
    }

    /**
     * 获取订单信息
     * @param orderId 订单ID
     * @param status 订单状态
     * @return 订单详细信息
     */
    @GetMapping("/orders/{orderId}")
    public OrdersInfoResponse getOrdersInfo(
            @PathVariable String orderId) {
        return ordersService.getOrdersInfo(orderId);
    }

    /**
     * 获取订单列表
     * @param tenantId 餐厅ID
     * @param status 订单状态（可选）
     * @return 订单列表
     */
    @GetMapping("/orders")
    public OrdersListResponse getOrdersList(
            @RequestParam String tenantId,
            @RequestParam(required = false) String status) {
        return ordersService.getOrdersList(tenantId, status);
    }

    /**
     * 更新订单信息
     * @param orderId 订单ID
     * @param status 新状态
     * @param resvId 预定ID（可选）
     * @param takeoutId 外带ID（可选）
     * @param sessionId 会话ID（可选）
     * @return 操作结果
     */
    @PostMapping("/orders/{orderId}")
    public CommonResponse updateOrdersInfo(
            @PathVariable String orderId,
            @RequestParam String status,
            @RequestParam(required = false) String resvId,
            @RequestParam(required = false) String takeoutId,
            @RequestParam(required = false) String sessionId) {
        return ordersService.updateOrdersInfo(orderId, status, resvId, takeoutId, sessionId);
    }
}
